/**
 * 员工档案数据持久化处理类
 * EmployeeDao.java
 * Copyright (C) 2013 Gank
 * Simple Currency Trade System
 * 
 */
package com.gank.ct.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.gank.ct.common.BaseDao;
import com.gank.ct.domain.Employee;
import com.gank.ct.util.ChkUtil;

/**
 * Copyright (C) 2013:Gank<br/>
 * Project:Curreny Trade<br/>
 * Module ID:<br/>
 * Comments:Simple Currency Trade System<br/>
 * JDK version used:JDK1.6<br/>
 * Package:com.gank.ct.dao<br/>
 * Author:Michael Wu<br/>
 * Create Date:2013-11-6<br/>
 * Modified By:<br/>
 * Modified Date:<br/>
 * Why & What is modified:<br/>
 * Version:0.1<br/>
 */
public class EmployeeDao extends BaseDao<Employee, Integer> implements
		IEmployeeDao {

	/*
	 * 员工档案查询
	 * 
	 * @see
	 * com.zds.slms.dao.IEmployeeDao#findEmployee(com.zds.slms.domain.Employee)
	 */
	public List<Employee> findEmployee(Employee employee) {
		// 对象查询条件
		DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
		if (null != employee) {
			if (!ChkUtil.isNullOrEmpty(employee.getId())) {
				criteria.add(Restrictions.eq("id", employee.getId()));
			}
			if (!ChkUtil.isNullOrEmpty(employee.getCode())) {
				criteria.add(Restrictions.eq("code", employee.getCode()));
			}
			if (!ChkUtil.isNullOrEmpty(employee.getName())) {
				criteria.add(Restrictions.like("name", employee.getName(),
						MatchMode.ANYWHERE));

			}
		}
		return findByCriteria(criteria);
	}

	/*
	 * 员工档案保存
	 * 
	 * @see
	 * com.zds.slms.dao.IEmployeeDao#saveEmployee(com.zds.slms.domain.Employee)
	 */
	public void saveEmployee(Employee employee) {
		save(employee);

	}

	/*
	 * 员工档案删除
	 * 
	 * @see com.zds.slms.dao.IEmployeeDao#deleteEmployee(java.lang.String[])
	 */
	public void deleteEmployee(String[] employeeId) {
		List<Employee> entities = new ArrayList<Employee>();
		for (String eid : employeeId) {
			entities.add(load(Integer.valueOf(eid)));
		}
		// 批量删除
		deleteAll(entities);

	}

	/*
	 * 员工档案更新
	 * 
	 * @see
	 * com.zds.slms.dao.IEmployeeDao#updateEmployee(com.zds.slms.domain.Employee
	 * )
	 */
	public void updateEmployee(Employee employee) {
		Integer eid = employee.getId();
		// 载入已经被持久化了的对象然后再进行修改
		Employee perstEmployee = load(Integer.valueOf(eid));
		perstEmployee.setBirthday(employee.getBirthday());
		perstEmployee.setCode(employee.getCode());
		perstEmployee.setEmail(employee.getEmail());
		perstEmployee.setName(employee.getName());
		perstEmployee.setSex(employee.isSex());
		perstEmployee.setTelephone(employee.getTelephone());
		update(perstEmployee);

	}

}
